package com.ag.admin.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.stereotype.Controller;

import com.ag.admin.service.impl.RoleServiceImpl;
import com.ag.bean.TbPermission;
import com.ag.bean.TbRole;
import com.ag.common.AjaxNotify;
import com.ag.common.BaseAction;
import com.ag.common.IDUtil;
import com.ag.common.IIDService;

@Controller("RoleAction")
public class RoleAction extends BaseAction implements ServletContextAware {
	private static final Logger logger = Logger.getLogger(RoleAction.class);
	private static final long serialVersionUID = 1254755643842522902L;

	@Resource(name = "roleServiceImpl")
	private RoleServiceImpl roleService;
	@Resource(name = "iDServiceImpl")
	private IIDService idServiceImpl;
	private TbRole role;
	private List<TbRole> roles;
	private List<TbPermission> permissions;

	/**
	 * 用户上下文
	 */
	private ServletContext servletContext;

	/**
	 * 角色ID
	 */
	private String RId;
	/**
	 * 角色权限字符串，以逗号分隔
	 */
	private String rolePmsnStr;

	/**
	 * 权限列表
	 */
	private List<TbPermission> pmsnList;

	public List<TbPermission> getPmsnList() {
		return pmsnList;
	}

	/**
	 * 异步请求结果返回
	 */
	private String ajaxRst;

	public String getAjaxRst() {
		return ajaxRst;
	}

	public void setRId(String rId) {
		RId = rId;
	}

	/**
	 * 根据ID删除角色
	 * 
	 * @return
	 */

	public String delRoleById() {
		try {
			roleService.delete(RId);
			ajaxRst = AjaxNotify.SUCCESS;
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	public String save() {
		if (!role.getRName().isEmpty()) {
			try {
				String RId = idServiceImpl.findTablePK(IDUtil.TB_ROLE);
				role.setRId(RId);
				role.setA(IDUtil.PRESERVE);
				if (this.roleService.save(role)) {
					ajaxRst = AjaxNotify.SUCCESS;
				} else {
					ajaxRst = AjaxNotify.EXCEPTION;
				}

			} catch (Exception e) {
				e.printStackTrace();
				ajaxRst = AjaxNotify.EXCEPTION;
			}
		}
		return "operate";
	}

	/**
	 * 通过角色ID保存角色权限
	 * 
	 * @return
	 */
	public String saveRolePmsnByRoleId() {
		try {
			roleService.saveRolePmsnByRoleId(RId, rolePmsnStr.trim(),
					servletContext);
			ajaxRst = AjaxNotify.SUCCESS;
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 通过角色ID查询所权限
	 * 
	 * @return
	 */
	public String findAllPmsnByRoleId() {
		permissions = this.roleService.findAllPmsnByRoleId(RId);
		return "findAllPermission";
	}

	public String findAllRole() {
		roles = this.roleService.findAllRole();
		return "findAllRole";
	}

	public String findAllPermission() {
		permissions = this.roleService.findAllPermission();
		return "findAllPermission";
	}

	public TbRole getRole() {
		return role;
	}

	public void setRole(TbRole role) {
		this.role = role;
	}

	public List<TbRole> getRoles() {
		return roles;
	}

	public void setRoles(List<TbRole> roles) {
		this.roles = roles;
	}

	public List<TbPermission> getPermissions() {
		return permissions;
	}

	public void setPermissions(List<TbPermission> permissions) {
		this.permissions = permissions;
	}

	public void setRolePmsnStr(String rolePmsnStr) {
		this.rolePmsnStr = rolePmsnStr;
	}

	@Override
	public void setServletContext(ServletContext arg0) {
		servletContext = arg0;
	}
}
